<%--
    Document   : index
    Created on : Oct 7, 2008, 8:50:36 AM
    Author     : nbuser
 
 SELECT expense_item_id, reference_number, expense_date, expense_type_id
         , payment_type_id, client_id, price, description, note, billable
    FROM expense_items
--%>

<%@page import="com.google.gdata.util.common.util.Base64"%>
<%@page import="com.google.gdata.data.extensions.Organization"%>
<%@page import="com.google.gdata.data.extensions.PostalAddress"%>
<%@page import="com.google.gdata.data.extensions.StructuredPostalAddress"%>
<%@page import="com.google.gdata.data.extensions.Email"%>
<%@page import="com.bitzer.google.Utils"%>
<%@page import="com.google.gdata.client.contacts.ContactsService"%>
<%@page import="java.io.IOException"%>
<%@page import="com.google.gdata.util.ServiceException"%>
<%@page import="com.google.gdata.data.contacts.ContactFeed"%>
<%@page import="com.google.gdata.data.contacts.ContactEntry"%>
<%@page import="java.util.List"%>
<%@page import="com.bitzer.google.Contacts"%>
<%@page import="com.bitzer.google.Constants"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="bitzer-component" content="data"/>

        <title>Contacts</title>
    </head>
    <body>
        <%

        String username = (String) session.getAttribute("username");
        String password = (String) session.getAttribute("password");

        if (username == null || username == ""
           || password == null || password == ""){
                        response.sendRedirect(request.getContextPath()+"/login.jsp");
            return;
            }
        
       Contacts contacts = Contacts.instance();

       String startRowStr = request.getParameter("startRow");
       String numberOfRowsStr = request.getParameter("numberOfRows");
       String lastModifiedOn = request.getParameter("lastModifiedOn");

       String   usernameBase64 = Base64.encode(username.getBytes());


       int startRow = Utils.stringToInt(startRowStr, 1);
       if (startRow == 0)
           startRow = 1;
       int numberOfRows = Utils.stringToInt(numberOfRowsStr, 50000);
       if (numberOfRows == 0){
           numberOfRows = 50000;
           }
      ContactFeed feed = null;
      try{
      feed = contacts.getFeed(lastModifiedOn, startRow, numberOfRows , request);
      }catch(Exception e){

      }
       %>

       <table data-bitzer-data="true"
		                       data-bitzer-title="ContactsE"
		                       data-bitzer-desc="List of contacts"
                                       data-bitzer-component="ContactsE"
                                       data-bitzer-record-count="<%=contacts.recordCount%>"
                                       data-bitzer-start-index="<%=startRow%>"
                                       data-bitzer-number-of-rows="<%=numberOfRows%>">
        <tr>
            <th data-bitzer-field="id" data-bitzer-key-order="1">id</th>
            <th data-bitzer-field="first_name">First Name</th>
            <th data-bitzer-field="last_name">Last Name</th>
            <th data-bitzer-field="title">Title</th>
            <th data-bitzer-field="photo">Photo</th>
            <th data-bitzer-field="pemail">Primary email</th>
            <th data-bitzer-field="hemail">Home email</th>
            <th data-bitzer-field="wemail">Work email</th>
            <th data-bitzer-field="oemail">Other email</th>
            <th data-bitzer-field="wphone">Work Phone</th>
            <th data-bitzer-field="cphone">Cell Phone</th>
            <th data-bitzer-field="hphone">Home Phone</th>

            <th data-bitzer-field="lastModifiedOn">Last Modified</th>
           </tr>
            <%
            try {

                String imageUrl = "";
                for (int i = 0; i < feed.getEntries().size(); i++) {
                    ContactEntry entry = feed.getEntries().get(i);
                    out.println(Utils.dataRowElement("id", entry.getId()));

                    out.print(Utils.dataCell(entry.getId()));

                    if (entry.hasName()) {
                       
                        if (entry.getName().hasGivenName())
                            out.println(Utils.dataCell(entry.getName().getGivenName().getValue()));
                        else
                            out.println(Utils.dataCell(""));

                        if (entry.getName().hasFamilyName())
                            out.println(Utils.dataCell(entry.getName().getFamilyName().getValue()));
                        else
                            out.println(Utils.dataCell(""));

                    } else {
                        out.println(Utils.dataCell(""));
                        out.println(Utils.dataCell(""));
                    }

                    String title = null;

                    if (entry.hasOrganizations()) {
                        Organization organization = Utils.getOrganization(entry.getOrganizations());
                        if (organization != null) {
                            if (organization.hasOrgTitle())
                                   title = organization.getOrgTitle().getValue();
                           
                        }
                    }

                    if (title != null)
                        out.print(Utils.dataCell(title));
                    else
                        out.print(Utils.dataCell(""));

                    imageUrl = Contacts.getPhoto(entry, request.getRealPath("/"), request.getContextPath() , usernameBase64);
                    
                    out.print(Utils.dataCell(imageUrl));
                    //out.print(Utils.dataCell("<img src=\""+imageUrl+"\"/>"));

                    out.print(Utils.dataCell(Utils.getPrimaryEmail(entry.getEmailAddresses())));
                    out.print(Utils.dataCell(Utils.getEmail(entry.getEmailAddresses(), Constants.HOME_REL)));
                    out.print(Utils.dataCell(Utils.getEmail(entry.getEmailAddresses(), Constants.WORK_REL)));
                    out.print(Utils.dataCell(Utils.getEmail(entry.getEmailAddresses(), Constants.OTHER_REL)));

                    if (entry.hasPhoneNumbers()) {
                        out.print(Utils.dataCell(Utils.getPhone(entry.getPhoneNumbers(), Constants.WORK_REL)));
                        out.print(Utils.dataCell(Utils.getPhone(entry.getPhoneNumbers(), Constants.MOBILE_REL)));
                        out.print(Utils.dataCell(Utils.getPhone(entry.getPhoneNumbers(), Constants.HOME_REL)));
                    } else {
                        out.println(Utils.dataCell(""));
                        out.println(Utils.dataCell(""));
                        out.println(Utils.dataCell(""));
                    }


                    //out.print(Utils.dataCell(entry.getUpdated().toStringRfc822()));
                    out.print(Utils.dataCell( Utils.formattedDateTime(entry.getUpdated()) ));
                    out.print("</tr>");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        %>
		                        
		                </table>							

    </body>
</html>
